package se.olli.jonsered;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryActivity extends Activity {
	private Intent viewer;
	private int dpi;
	public final static String IMAGE_ID = "IMAGE_ID", IMG_ARRAY = "IMG_ARRAY";
		private int[] images = {
				R.drawable.gallery0,
				R.drawable.gallery1, R.drawable.gallery2, R.drawable.gallery3,
				R.drawable.gallery4, R.drawable.gallery5, R.drawable.gallery6,
				R.drawable.gallery7, R.drawable.gallery8, R.drawable.gallery9
		}, thumbs = {
				R.drawable.thumb_gallery0,
				R.drawable.thumb_gallery1, R.drawable.thumb_gallery2, R.drawable.thumb_gallery3,
				R.drawable.thumb_gallery4, R.drawable.thumb_gallery5, R.drawable.thumb_gallery6,
				R.drawable.thumb_gallery7, R.drawable.thumb_gallery8, R.drawable.thumb_gallery9
		};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		
		dpi = this.getResources().getDisplayMetrics().densityDpi;
		
		viewer = new Intent(this, ImageViewerActivity.class);

		GridView grid = (GridView) findViewById(R.id.gallery_gridview);
		grid.setAdapter(new ImageAdapter(this, thumbs, dpi));

		grid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				viewer.putExtra(IMAGE_ID, position);
				viewer.putExtra(IMG_ARRAY, images);
				startActivity(viewer);
			}
		});
	}

}

class ImageAdapter extends BaseAdapter {
	private Context mContext;
	private int[] images;
	private int dpi;
	public ImageAdapter(Context c, int[] im, int d) {
		mContext = c;
		images = im;
		dpi = d;
	}

	public int getCount() {
		return images.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) {  // if it's not recycled, initialize some attributes
			imageView = new ImageView(mContext);
			if(dpi > 250)
				imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
			else 
				imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			//imageView.setPadding(2, 2, 2, 2);
			//imageView.setBackgroundResource(R.drawable.img_border);
		} else {
			imageView = (ImageView) convertView;
		}

		imageView.setImageResource(images[position]);
		return imageView;
	}
	
}